//
//  MTLiveManagerListController.m
//  MeToo
//
//  Created by 俊松何 on 2018/9/26.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLiveManagerListController.h"
#import "MTManagerListCell.h"
#import "MTLiveSearchUserController.h"

@interface MTLiveManagerListController ()<MTManagerListCellDelegate>
@property(nonatomic,strong) NSMutableArray *dataSource;

@property(nonatomic,assign) int page;


@end

@implementation MTLiveManagerListController
//1,管理员列表 2，黑名单列表 3，禁言列表
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MTManagerListCell" bundle:nil] forCellReuseIdentifier:@"MTManagerListCellID"];
    self.tableView.rowHeight = 60;
    self.tableView.tableFooterView = [UIView new];
    self.page = 1;
    if(self.type == 1)
    {
        self.title = @"管理员列表";
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        [but setTitle:@"添加管理员" forState:UIControlStateNormal];
        but.titleLabel.font = [UIFont systemFontOfSize:15];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [but addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [but sizeToFit];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
        self.navigationItem.rightMargin = 10;
        @weakify(self);
        // 下拉刷刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.page = 1;
            [self getDataIsHeaderRefresh:YES];
        }];
        
        [self.tableView emptyViewConfigerBlock:^(FOREmptyAssistantConfiger *configer) {
            configer.emptySubtitle = NSLTLocalizedString(@"暂无管理员，赶快添加吧！", nil);
            configer.emptySubtitleFont = [UIFont systemFontOfSize:14];
            configer.emptySubtitleColor = UIColorFromRGB(0xcccccc);
            configer.emptyImage = [UIImage imageNamed:@"EmptyBox"];
            configer.shouldDisplay = ^{
                return YES;
            };
        }];
        
    }else if(self.type == 2){
        self.title = @"黑名单列表";
        @weakify(self);
        // 下拉刷刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.page = 1;
            [self getDataIsHeaderRefresh:YES];
        }];
        
        // 上拉加载更多
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            self.page += 1;
            [self getDataIsHeaderRefresh:NO];
        }];
        
        [self.tableView emptyViewConfigerBlock:^(FOREmptyAssistantConfiger *configer) {
            configer.emptySubtitle = NSLTLocalizedString(@"暂无黑名单人员", nil);
            configer.emptySubtitleFont = [UIFont systemFontOfSize:14];
            configer.emptySubtitleColor = UIColorFromRGB(0xcccccc);
            configer.emptyImage = [UIImage imageNamed:@"EmptyBox"];
            configer.shouldDisplay = ^{
                return YES;
            };
        }];
        self.tableView.mj_footer.automaticallyHidden = YES;
        
    }else if (self.type == 3)
    {
        self.title = @"禁言列表";
        @weakify(self);
        // 下拉刷刷新
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.page = 1;
            [self getDataIsHeaderRefresh:YES];
        }];
        
        // 上拉加载更多
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            self.page += 1;
            [self getDataIsHeaderRefresh:NO];
        }];
        
        [self.tableView emptyViewConfigerBlock:^(FOREmptyAssistantConfiger *configer) {
            configer.emptySubtitle = NSLTLocalizedString(@"暂无禁言人员", nil);
            configer.emptySubtitleFont = [UIFont systemFontOfSize:14];
            configer.emptySubtitleColor = UIColorFromRGB(0xcccccc);
            configer.emptyImage = [UIImage imageNamed:@"EmptyBox"];
            configer.shouldDisplay = ^{
                return YES;
            };
        }];
        self.tableView.mj_footer.automaticallyHidden = YES;
    }
    
   
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     [self getDataIsHeaderRefresh:YES];
    
}


-(void)addClick{
    
    MTLiveSearchUserController *searchVc = [MTLiveSearchUserController baseStoryBordWithName:@"live"];
    searchVc.liveVc = self.liveVc;
    [self presentViewController:searchVc animated:true completion:nil];
    
}


#pragma mark - 请求所有数据方法
- (void)getDataIsHeaderRefresh:(BOOL)headerRefresh {
    
   void(^block)(MTUserLiveTotalModel *livetotalModel,MTError *error) = ^(MTUserLiveTotalModel *livetotalModel,MTError *error) {
       if(error == nil)
       {
           if (headerRefresh) {
               self.dataSource = livetotalModel.list;
               [self.tableView reloadData];
           } else {
               [self.dataSource addObjectsFromArray:livetotalModel.list];
               [self.tableView reloadData];
           }
           if (livetotalModel.last) {
               [self.tableView.mj_footer endRefreshingWithNoMoreData];
           } else {
               [self.tableView.mj_footer resetNoMoreData];
               [self.tableView.mj_footer endRefreshing];
           }
           [self.tableView.mj_header endRefreshing];
       }
       if (headerRefresh) {
           [self.tableView.mj_header endRefreshing];
       }else
       {
           [self.tableView.mj_footer endRefreshing];
       }
   };
    
    
    if(self.type == 1)
    {
        [[MTUserApiManager manager] getMamgerUserList:^(NSMutableArray<LTUser *> *managerUsers, MTError *mterror) {
              [self.tableView.mj_header endRefreshing];
            if(mterror == nil)
            {
                self.dataSource = managerUsers;
                [self.tableView reloadData];
            }
        }];
    }
    else if(self.type == 2)
    {
        [[MTUserApiManager manager] getBlackUserListPage:self.page masterUserId:self.liveUserId complation:block];
    }else if (self.type == 3)
    {
        [[MTUserApiManager manager] getnoSpeakUserListPage:self.page masterUserId:self.liveUserId complation:block];
    }
    

}




#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTManagerListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MTManagerListCellID"];
    cell.user = self.dataSource[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)deleteUser:(LTUser *)user
{
    
    if(self.type == 1)
    {
        [MBProgressHUD showProcessHud:@"" toView:nil];
        [[MTUserApiManager manager] deleteLiveRoomManagertargetUserId:user.userId completion:^(MTError *mterror) {
            [MBProgressHUD hideHUDForView:nil];
            if(mterror == nil)
            {
                [self.dataSource removeObject:user];
                [self.tableView reloadData];
                [MBProgressHUD showSuccess:@"解除成功"];
                [self.liveVc mtLiveProfileViewCancelMamnager:user];
            }
        }];
    }else if(self.type == 3){
        [MBProgressHUD showProcessHud:@"" toView:nil];
        [[MTUserApiManager manager] deleteNoSpeaktargetUserId:user.userId masterUserId:self.liveUserId completion:^(MTError *mterror) {
            [MBProgressHUD hideHUDForView:nil];
            if(mterror == nil)
            {
                [self.dataSource removeObject:user];
                [self.tableView reloadData];
                [MBProgressHUD showSuccess:@"解除成功"];
                [self.liveVc mtLiveProfileViewCnacelMite:user];
            }
        }];
    }else if(self.type == 2)
    {
         [MBProgressHUD showProcessHud:@"" toView:nil];
        [[MTUserApiManager manager] deleteBlackUsertargetUserId:user.userId masterUserId:self.liveUserId completion:^(MTError *mterror) {
           
            [MBProgressHUD hideHUDForView:nil];
            if(mterror == nil)
            {
                [self.dataSource removeObject:user];
                [self.tableView reloadData];
                [MBProgressHUD showSuccess:@"解除成功"];
            }
        }];
    }
    
}


////让tableView可编辑
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
//
//- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //删除
//    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
//        NSLog(@"点击了删除");
//        LTUser *user = self.dataSource[indexPath.row];
//
//        [[MTUserApiManager manager] deleteLiveRoomManagertargetUserId:user.userId completion:^(MTError *mterror) {
//           if(mterror == nil)
//           {
//               [self.dataSource removeObject:user];
//               [self.tableView reloadData];
//           }
//        }];
//
//
//    }];
//    return @[deleteRowAction];
//}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
